package com.wangsanfei.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 日期公共类
 * 
 * @author wanghu
 * @since 1.0
 */
public class DateUtils {

	private static final Logger logger = Logger.getLogger(DateUtils.class);
	private static final String DEFAULT_PATTERN = "yyyy-MM-dd";
	private static final int DEFAULT_NUMBER = 0;
	private static final String DIFF_DAY = "d";
	private static final String DIFF_HOUR = "h";
	private static final String DIFF_MIN = "m";
	
	
	public static Date parse(String date) {
		return parse(date, DEFAULT_PATTERN);
	}

	public static Date parse(String date, String pattern) {
		try {
			SimpleDateFormat defaultFormat = new SimpleDateFormat(pattern);
			return defaultFormat.parse(date);
		} catch (ParseException e) {
			logger.debug(e.getMessage());
			return null;
		}
	}

	public static String format(Date date) {
		return format(date, DEFAULT_PATTERN);
	}

	public static String format(Date date, String pattern) {
		SimpleDateFormat defaultFormat = new SimpleDateFormat(pattern);
		return defaultFormat.format(date);
	}

	public static String getBeforeYearMonth(Date date, String pattern) {
		return getBeforeYearMonth(date, pattern, DEFAULT_NUMBER);
	}

	/**
	 * 返回n个月时间
	 * 
	 * @param date
	 *            2014-01-01
	 * @pattern 格式 化时间 yyyy-MM-dd 或yyyyMM
	 * @n 返回n月
	 * @return sring 201312
	 */
	public static String getBeforeYearMonth(Date date, String pattern, int n) {
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		} else {
			date = calendar.getTime();
		}
		calendar.add(Calendar.MONTH, n);
		date = calendar.getTime();
		return format(date, pattern);
	}

	/**
	 * 返回比date多addDay天的时间
	 * 
	 * @param date
	 * @param addDay
	 * @return
	 */
	public static Date getDateAddDay(Date date, int addDay) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, addDay);
		Date result = calendar.getTime();
		calendar.clear();
		return result;
	}

	/**
	 * 返回2个时间相差天数或小时数或分钟数,如果第二个参数比第一个参数大，返回0.
	 * @param pBeginTime 较小的时间
	 * @param pEndTime 较大的时间
	 * @param type d表示返回天数   h表示小时数  m表示分钟数
	 * @return 
	 */
	public static Long timeDiff(Date pBeginTime, Date pEndTime,String type){
		if(pEndTime!=null){
			if(pEndTime.before(pBeginTime)){
				return 0L;
			}
			Long beginL = pBeginTime.getTime();
			Long endL = pEndTime.getTime();
			Long day = (endL - beginL) / 86400000;
			Long hour = ((endL - beginL) % 86400000) / 3600000;
			Long min = ((endL - beginL) % 86400000 % 3600000) / 60000;
			if(type.equals(DIFF_DAY)){
				return day;
			}
			if(type.equals(DIFF_HOUR)){
				return day*24+hour;
			}
			if(type.equals(DIFF_MIN)){
				return (day*24+hour)*60+min;
			}
			return 0L;
		}
		return 0L;
	}
	
	public static void main(String[] args) throws ParseException {
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str1 = "2012-02-01";
		String str2 = "2013-03-01";

		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		bef.setTime(sdf.parse(str1));
		aft.setTime(sdf.parse(str2));
		
		int start=(bef.get(Calendar.YEAR)-1970)*12+bef.get(Calendar.MONTH);
		int end=(aft.get(Calendar.YEAR)-1970)*12+aft.get(Calendar.MONTH);
		
		System.out.println(Math.abs(end-start));
		
	}

}
